package com.fengwan.wastesort.ui.fragment

import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter

import com.fengwan.wastesort.R
import com.fengwan.wastesort.adapter.WasteSortAdapter
import com.fengwan.wastesort.adapter.WasteSortAdapter2
import com.fengwan.wastesort.model.Waste
import com.fengwan.wastesort.ui.viewModel.WasteCycleViewModel
import kotlinx.android.synthetic.main.fragment_waste_sort.*

private const val HEADER_COLOR = "headerColor"
private const val HEADER_TITLE = "headerTitle"
private const val HEADER_TITLE_IMAGE = "headerTitleImage"
private const val HEADER_SUB_TITLE = "headerSubtitle"
private const val WASTE_TYPE = "wasteType"

class WasteSortFragment : BaseWasteFragment(), BaseQuickAdapter.RequestLoadMoreListener, Observer<List<Waste>> {

    private var headerColors =
        arrayListOf(R.color.colorWasteDry, R.color.colorWasteWet, R.color.colorWasteCycle, R.color.colorWasteHarmful)
    private var headerTitles = arrayListOf("干垃圾", "湿垃圾", "可回收垃圾", "有害垃圾")
    private var headerTitleImages =
        arrayListOf(R.mipmap.icon_residual, R.mipmap.icon_recylable, R.mipmap.icon_household, R.mipmap.icon_hazardous)
    private var headerSubtitles = arrayListOf(
        "除其他三类垃圾外的生活废弃物\n如废弃纸巾、尿不湿、烟蒂、干燥剂、污损塑料、大骨头、硬贝壳、毛发、橡皮泥、旧毛巾、一次性餐具、镜子、陶瓷制品、竹h制品、成分复杂的制品等。\n◆易碎或已污染的、应包裹后投放\n◆投放时，应保持周边环境整洁\n◆与其他种类垃圾分开投放",
        "即易腐垃圾，易腐的生物质生活废弃物\n如食物废料、剩饭剩菜、过期食品、瓜皮果核、花卉植物。\n◆沥干水分\n◆有外包装的应去除外包装\n◆与其他种类垃圾分开投放",
        "指适宜回收可循环利用的废弃物\n如废纸张、废塑料、废玻璃制品、废金属、废织物、其他(电路板、电线、插座、木积木、砧板)。\n◆投放时，应尽量保持清洁干燥，避免污染\n◆立体包装应清空内容物，清洁后压扁投放\n◆易破损或者有裹尖锐边角的应包后投放",
        "指对人体健康或者自然环境造成直接或潜在危害的废弃物\n如废镍镉电池和废氢化汞电池、废银光灯管、废药品及其包装物、废油漆和溶剂及其包装物、废含汞温度计和血压计、废杀虫剂消毒剂及其包装物、废胶片及其相纸。\n◆分类投放有害垃圾时，应注意轻放\n◆易碎尖锐的，应连带包装或包裹后投放\n◆压力罐装容器，应排空内容物后投放"
    )
    private var wasteType: Int = 0


    companion object {
        fun newInstance(
            wasteType: Int
            ) = WasteSortFragment().apply {
            arguments = Bundle().apply {
                putInt(WASTE_TYPE, wasteType)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        arguments?.let {
            wasteType = it.getInt(WASTE_TYPE)
        }
    }

    private val adapter by lazy { WasteSortAdapter2(activity!!) }
    private val viewModel by lazy { ViewModelProviders.of(this).get(WasteCycleViewModel::class.java) }
    private var isFirst = true

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_waste_sort, container, false)
    }

    override fun initListener() {
        viewModel.cycleWastes.observe(this, this)
    }

    override fun initView(view: View) {
        recycler.layoutManager = GridLayoutManager(context, 2, RecyclerView.VERTICAL, false)
        when (wasteType) {
            0 -> adapter.setHeaderData(headerColors[0], headerTitles[0], headerTitleImages[0], headerSubtitles[0])
            1 -> adapter.setHeaderData(headerColors[1], headerTitles[1], headerTitleImages[1], headerSubtitles[1])
            2 -> adapter.setHeaderData(headerColors[2], headerTitles[2], headerTitleImages[2], headerSubtitles[2])
            3 -> adapter.setHeaderData(headerColors[3], headerTitles[3], headerTitleImages[3], headerSubtitles[3])
            else -> adapter.setHeaderData(headerColors[0], headerTitles[0], headerTitleImages[0], headerSubtitles[0])
        }
        recycler.adapter = adapter
    }

    override fun loadData(_id: Int, type: Int) {
        viewModel.loadCycleWastes(_id, 1)
    }

    override fun onChanged(it: List<Waste>) {
        if (it.any()) {
            if (isFirst) {
                adapter.setNewData(it)
                isFirst = false
            } else {
                adapter.addData(it)
            }
//            adapter.loadMoreComplete()
        } else {
//            adapter.loadMoreEnd(true)
        }
    }

    override fun onDestroyView() {
        viewModel.cycleWastes.removeObserver(this)
        super.onDestroyView()
    }

    override fun onLoadMoreRequested() {
//        loadData(adapter.data[adapter.data.size - 1]._id)
    }


}
